package com.neu.his.controller.SystemManage01.DepartmentManage;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neu.his.core.Result;
import com.neu.his.entity.VO.SystemManage01.DepartmentManage.DepartmentDeleteVO;
import com.neu.his.entity.VO.SystemManage01.DepartmentManage.DepartmentVO;
import com.neu.his.entity.request.Department.DepartmentChange;
import com.neu.his.entity.request.Department.DepartmentDelete;
import com.neu.his.entity.request.Department.DepartmentPageParams;
import com.neu.his.entity.request.Department.DepartmentParams;
import com.neu.his.mapper.DepartmentMapper;
import com.neu.his.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description: neu-his
 * <p>
 * Created by 31316 on 2020/7/15 12:25
 */
@RestController
@RequestMapping("/department")
public class DepartmentManageController {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private DepartmentService departmentService;

    @PostMapping("/query")
    public Result<IPage<DepartmentVO>> getDepartmentPage(@RequestBody DepartmentPageParams departmentPageParams){
        Page<DepartmentVO> page=new Page<>();
        page.setCurrent(departmentPageParams.getCurPage());
        page.setSize(departmentPageParams.getPageSize());
        DepartmentParams departmentParams=new DepartmentParams();
        departmentParams.copy(departmentPageParams);
        IPage<DepartmentVO> iPage= departmentMapper.getDepartmentPage(page,departmentParams);
        return  Result.success(iPage);
    }

    @GetMapping("/all")
    public Result<List<DepartmentVO>> getDepartmentAll(){
        List<DepartmentVO> list =departmentMapper.getDepartment(new DepartmentParams());
        return Result.success(list);
    }

    @PostMapping
    public Result insertOne(@RequestBody DepartmentParams departmentParams){
        boolean bool=departmentService.insertOne(departmentParams);
        if(bool) return Result.success();
        else return Result.fail();
    }

    @PutMapping
    public Result changeOne(@RequestBody DepartmentChange departmentChange){
        boolean bool=departmentService.changeOneById(departmentChange);
        if(bool) return Result.success();
        else return Result.fail();
    }

    @DeleteMapping
    public Result<DepartmentDeleteVO> deleteSome(@RequestBody DepartmentDelete departmentDelete){
        departmentMapper.deleteByIds(departmentDelete.getIds());
        return Result.success(new DepartmentDeleteVO());
    }
}
